package com.lebang.service;

import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.MemberIdentityInfo;
import com.lebang.example.MemberIdentityInfoExample;
import com.lebang.mapper.MemberCashWithdrawalMapper;
import com.lebang.mapper.MemberIdentityInfoMapper;
import com.lebang.model.enums.Gender;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @Author: xwj
 * @Date: 2020/05/28 14:28
 * @Version 1.0
 */
@Service
public class MemberIdentityInfoService extends BaseService<MemberIdentityInfo, Integer, MemberIdentityInfoExample> {

    @Autowired
    private MemberIdentityInfoMapper memberIdentityInfoMapper;
    @Autowired
    private MemberCashWithdrawalMapper memberCashWithdrawalMapper;

    @DataSource("commonDB")
    public MemberIdentityInfo selectOneByCard(String iCard,String name) {
       return  memberIdentityInfoMapper.selectOneByCard(iCard,name);
    }

    /**
     * 用户身份信息写入表内
     * @param icard
     * @param name
     * @return
     */
    public void insertInfo(String icard, String name) {
        MemberIdentityInfo memberIdentityInfo = new MemberIdentityInfo();
        memberIdentityInfo.setCreateTime(LocalDateTime.now());
        memberIdentityInfo.setModifyTime(LocalDateTime.now());
        memberIdentityInfo.setHasDeleted(0);
        memberIdentityInfo.setRealName(name);
        memberIdentityInfo.setIcard(icard);
        memberIdentityInfo.setIcardCode(icard.substring(0, 6));
        if (NumberUtils.createInteger(icard.substring(16, 17)) % 2 == 0) {
            memberIdentityInfo.setSex(Gender.Female.getTitle());
        } else {
            memberIdentityInfo.setSex(Gender.Male.getTitle());
        }
        int i = Integer.parseInt(icard.substring(6, 10));
        memberIdentityInfo.setBirthday(String.valueOf(LocalDate.now().getYear() - i));
        memberIdentityInfo.setCodeAddr(memberCashWithdrawalMapper.selectAddrByCode(icard.substring(0, 6)));
        memberIdentityInfoMapper.insertOne(memberIdentityInfo);
    }
}
